﻿using AuthCenter.Api.Read.Application.Command.RoleMenu;
using AuthCenter.Api.Read.Application.Output;
using AuthCenter.Application;
using AuthCenter.Common.Common;
using AuthCenter.Domain;
using AuthCenter.ErrorCode;
using AutoMapper;
using MediatR;
using Newtonsoft.Json.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AuthCenter.Api.Read.Application.Hander.RoleMenu
{
    public class LoginHander:IRequestHandler<LoginCommand,ApiResult<UserLoginOutput>>
    {
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IBaseRepository<RoleMenuModel> _roleMenuRepository;
        private readonly IBaseRepository<MenuModel> _menuRepository;
        private readonly IBaseRepository<UserModel> _userRepository;
        private readonly IBaseRepository<UserRoleModel> _userroleRepository;
        private readonly JwtTools _jwtTools;
        private readonly IMapper _mapper;
        private readonly IConfiguration _configuration;
        public LoginHander(IBaseRepository<RoleModel> roleRepository, IBaseRepository<RoleMenuModel> roleMenuRepository, IMapper mapper, IBaseRepository<MenuModel> menuRepository, IBaseRepository<UserModel> userRepository, IBaseRepository<UserRoleModel> userroleRepository, JwtTools jwtTools, IConfiguration configuration)
        {
            _roleRepository = roleRepository;
            _roleMenuRepository = roleMenuRepository;
            _mapper = mapper;
            _menuRepository = menuRepository;
            _userRepository = userRepository;
            _userroleRepository = userroleRepository;
            _jwtTools = jwtTools;
            _configuration = configuration;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task<ApiResult<UserLoginOutput>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            ApiResult<UserLoginOutput> res = new ApiResult<UserLoginOutput>();
            var user = _userRepository.GetAll().FirstOrDefault(x => x.UserName == request.UserName && x.Password == request.Password && !x.IsDeleted);
            if (user == null)
            {
                res.Code = ApiEnums.Error;
                res.Message = "用户或者密码不存在";
                return Task.FromResult(res);
            }
            var userrole = _userroleRepository.GetAll().Where(x => x.UserId == user.Id && !x.IsDeleted).ToList();
            if (userrole.Count() == 0)
            {
                res.Code = ApiEnums.Error;
                res.Message = "用户没有角色";
                return Task.FromResult(res);
            }
            var roleIds = new List<long>();
            var roles = new List<string>();
            foreach (var item in userrole)
            {
                roleIds.Add(item.RoleId);
                //获取角色详细信息
                var role = _roleRepository.GetAll()
                    .Where(x => x.Id == item.RoleId && !x.IsDeleted)
                    .FirstOrDefault();
                if (role != null)
                {
                    roles.Add(role.RoleName);
                }
            }
            roles = roles.Distinct().ToList();
            var department = _userRepository.GetAll().Where(x => x.DepartmentId == user.DepartmentId && !x.IsDeleted).Select(x => x.Id).ToList();
            var departmentids=new List<long>();
            foreach (var item in department)
            {
                var model = _userRepository.GetAll().Where(x => x.Id == item && !x.IsDeleted).FirstOrDefault();
                if (model == null)
                {
                    continue;
                }
                departmentids.Add(model.Id);
            }
            departmentids=departmentids.Distinct().ToList();
            var managernames=new List<string>();
            foreach (var item in departmentids)
            {
                var models = _userRepository.GetAll().Where(x => x.Id == item && !x.IsDeleted&&x.IsDepartmentName==true).FirstOrDefault();
                if (models == null)
                {
                    continue;
                }
                managernames.Add(models.UserName);
            }
            managernames= managernames.Distinct().ToList();
            var login = new UserLoginOutput
            {
                Id = user.Id,
                UserName = user.UserName,
                RoleName = roles,
                RoleId = roleIds,
                DepartmentId = user.DepartmentId,
                DepartmentIds = departmentids,
                ManagerName = managernames,
                Token = _jwtTools.JwtNweString(_configuration, user.Id.ToString())
            };
            res.Code = ApiEnums.OK;
            res.Message = "登录成功";
            res.Data = login;
            return Task.FromResult(res);
        }
    }
}
